package studentinformationmanagementsystem.servlet.admin;

import com.google.gson.Gson;
import studentinformationmanagementsystem.entity.ClassSchedule;
import studentinformationmanagementsystem.entity.Course;
import studentinformationmanagementsystem.entity.Major;
import studentinformationmanagementsystem.entity.Teacher;
import studentinformationmanagementsystem.service.ClassScheduleService;
import studentinformationmanagementsystem.service.CourseService;
import studentinformationmanagementsystem.service.MajorService;
import studentinformationmanagementsystem.service.TeacherService;
import studentinformationmanagementsystem.service.impl.ClassScheduleServiceImpl;
import studentinformationmanagementsystem.service.impl.CourseServicelmpl;
import studentinformationmanagementsystem.service.impl.MajorServicelmpl;
import studentinformationmanagementsystem.service.impl.TeacherServiceImpl;
import studentinformationmanagementsystem.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author 苗庆
 * @version 1.0
 */

@WebServlet("/admin/function/ClassSchedule.do")
public class ClassScheduleServlet extends BaseServlet {


    TeacherService teacherService = new TeacherServiceImpl();
    CourseService courseService = new CourseServicelmpl();
    MajorService majorService = new MajorServicelmpl();
    ClassScheduleService classScheduleService = new ClassScheduleServiceImpl();

    //查询所有的教师用户的教师编号
    public void queryAllTeachersworknumber(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Teacher> teachers = teacherService.queryAll();
        Gson gson = new Gson();
        String s = gson.toJson(teachers);
        response.getWriter().write(s);
    }

    //查询所有课程的课程编号
  public void   queryAllcourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      List<Course> courses = courseService.queryAll();
      Gson gson = new Gson();
      String s = gson.toJson(courses);
      response.getWriter().write(s);
  }

    //查询所有专业
    public void queryAllMajor (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Major> majors = majorService.queryAll();
        Gson gson = new Gson();
        String s = gson.toJson(majors);
        response.getWriter().write(s);
    }

    //添加课程信息
    public void addClassSchedule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                String whichDay01 = request.getParameter("whichDay01");
                String startingTime = request.getParameter("startingTime");
                String endTime = request.getParameter("endTime");
                String singleAndDoubleWeek = request.getParameter("singleAndDoubleWeek");
                String timeOfDuration = request.getParameter("timeOfDuration");
                String teacherId = request.getParameter("teacherId");
                String placeOfClass = request.getParameter("placeOfClass");
                String courseid = request.getParameter("courseid");
                String curriculumTime = request.getParameter("curriculumTime");
                String courseEndTime = request.getParameter("courseEndTime");
                String attendClassObject = request.getParameter("attendClassObject");
                String stidentmajor = request.getParameter("stidentmajor");

                //对添加内容再次进行非空校验和格式校验






                ClassSchedule classSchedule = new ClassSchedule(null,whichDay01,startingTime,endTime,singleAndDoubleWeek,timeOfDuration,Integer.valueOf(teacherId),placeOfClass,Integer.valueOf(courseid),curriculumTime,
                courseEndTime,attendClassObject,0,stidentmajor);

                //添加课表信息
                Map<String, String> map = classScheduleService.insert(classSchedule);



        if (map.get("code").equals("0")){  //选修课添加成功

            response.getWriter().write("0");

        }else if(map.get("code").equals("1")){//选修课添加失败

            response.getWriter().write("1");

        }else if (map.get("code").equals("2")){  //必修课添加成功

            response.getWriter().write("2");

        } else{                                  //必修课添加失败
            response.getWriter().write("3");
        }
    }

    //查询所有课程信息
    public void queryClassSchedule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<ClassSchedule> classSchedules = classScheduleService.queryAll();
        request.setAttribute("classSchedules",classSchedules);
        request.getRequestDispatcher("/admin/function/quall-class-schedule.jsp").forward(request,response);

    }

    //删除课程表信息
    public void deleteClassSchedule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        String classScheduleId = request.getParameter("classScheduleId");
        Map<String, String> map = classScheduleService.deleteClassSchedule(Integer.valueOf(classScheduleId));

        if (map.get("code").equals("0")){  //此课程已经开始上课，无法进行删除

            response.getWriter().print(0);

        }else if(map.get("code").equals("1")){//课表删除成功

            response.getWriter().print(1);

        }else if (map.get("code").equals("2")){  //课表删除失败

            response.getWriter().print(2);

        }

    }


    //修改课表信息1:查询这个课程的信息，进行回填
    public void queryClassScheduleid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String classScheduleId = request.getParameter("classScheduleId");
        //将修改信息放入Session中暂时存起来
        request.getSession().setAttribute("classScheduleId",classScheduleId);
        response.getWriter().print(0);
    }

    //修改课程表信息2：将需要修改的信息进行回填
    public void queryClassScheduleid01(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String classScheduleId = (String)request.getSession().getAttribute("classScheduleId");

        if (classScheduleId!=null){
            //查询需要修个的课程表信息
            ClassSchedule classSchedule = classScheduleService.queryAllid(classScheduleId);
            Gson gson = new Gson();
            String s = gson.toJson(classSchedule);
            response.getWriter().write(s);
        }else {
            response.getWriter().write("0");
        }

    }


    //修改课程表信息3：
    public void reviseClassScheduleid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String whichDay01 = request.getParameter("whichDay01");
        String startingTime = request.getParameter("startingTime");
        String endTime = request.getParameter("endTime");
        String singleAndDoubleWeek = request.getParameter("singleAndDoubleWeek");
        String timeOfDuration = request.getParameter("timeOfDuration");
        String placeOfClass = request.getParameter("placeOfClass");
        String classScheduleid = request.getParameter("classScheduleid");

        ClassSchedule classSchedule = new ClassSchedule();
        classSchedule.setClassScheduleId(Integer.valueOf(classScheduleid));
        classSchedule.setWhichDay(whichDay01);
        classSchedule.setStartingTime(startingTime);
        classSchedule.setEndTime(endTime);
        classSchedule.setSingleAndDoubleWeek(singleAndDoubleWeek);
        classSchedule.setTimeOfDuration(timeOfDuration);
        classSchedule.setPlaceOfClass(placeOfClass);

        Boolean aBoolean = classScheduleService.reviseClassScheduleid(classSchedule);

        //修改成功
        if (aBoolean){
            response.getWriter().print(0);
        }else {
            response.getWriter().print(1);
        }
    }


}
